package com.mairuide._frame.utils.excellistener;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.mairuide._frame.utils.ReflectUtils;
import com.mairuide._frame.utils.SpringUtil;
import com.mairuide.bill.information.PersonInformation;
import com.mairuide.bill.information.PersonInformationService;
import com.mairuide.bill.utils.DateUtils;
import com.mairuide.exceltitle_dict.enum2.FieldTypeEnum;
import com.mairuide.exceltitle_dict.enum2.PackageNameEnum;
import com.mairuide.exceltitle_dict.pojo.ExceltitleDict;
import com.mairuide.exceltitle_dict.service.ExceltitleDictService;

/**
  * 客户基本信息表（资金查控平台）
 * @author mh
 *
 */
public class CustomerInformationExcelListener extends AnalysisEventListener {
    private List<PersonInformation>  data = new ArrayList<PersonInformation>();
    private Map<Integer, ExceltitleDict> exceltitleDictMap = new HashMap<>();
    private Map<String,Object> resultMap = new HashMap<String,Object>();  //结果MAP
    private Map<String,Object> paraMap = new HashMap<String,Object>();  //参数MAP
    private List<String> TradeCardcollect=new ArrayList<String>();
    @SuppressWarnings("rawtypes")
	@Override
    public void invoke(Object object, AnalysisContext context) {
        String pattern = "\\d+";
        List<String> s = (List<String>)object;
        Integer rowNum = context.getCurrentRowNum();
        if(rowNum==0){
            exceltitleDictMap = getExceltitleDictByTitleHead(s);
            return;
        }
        if (exceltitleDictMap.size()==0) return;

        PersonInformation personInformation = new PersonInformation();
        for (int i=0;i<s.size();i++){
            if (s.get(i)==null) continue;
            Object content = s.get(i).trim();
            if(!exceltitleDictMap.containsKey(i)) continue;
            ExceltitleDict exceltitleDict = exceltitleDictMap.get(i);
            if (exceltitleDict==null) break;
            if (StringUtils.isBlank(s.get(i))) continue;
            String classField = exceltitleDict.getClassField();
            Class aClass = FieldTypeEnum.getFieldTypeEnumByVal(exceltitleDict.getFieldType());
            if (aClass.equals(Timestamp.class) ){
                Date date = DateUtils.getDate((String) content);
                if (data!=null){
                    content = new Timestamp(date.getTime());
                }
            }else if (aClass.equals(BigDecimal.class) ){
                content=new BigDecimal((String) content);
            }else if (aClass.equals(Integer.class) ){
                content=Integer.valueOf(content.toString());
            }
            ReflectUtils.setValue(personInformation,PersonInformation.class,classField,aClass,content);
        }
        personInformation.setCasemanagerId((String)paraMap.get("uid"));
        data.add(personInformation);
        if(data.size() >= 100){
            doSomething();
            Integer size=0;
            if (resultMap.containsKey("successNum")){
                size = (Integer) resultMap.get("successNum");
                size+=data.size();
            }else{
                size=data.size();
            }
            resultMap.put("successNum",size);
            data = new ArrayList<PersonInformation>();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        Integer size=0;
        if (resultMap.containsKey("successNum")){
            size = (Integer) resultMap.get("successNum");
            size+=data.size();
        }else{
            size=data.size();
        }
        resultMap.put("successNum",size);
        doSomething();
        data = new ArrayList<PersonInformation>();
    }
    public void doSomething(){
    	PersonInformationService personInformationService = SpringUtil.getBean(PersonInformationService.class);
    	personInformationService.batchSave(data);
    }


    /**
     * 根据标题头返回对应的数据库的行对象
     * @param titles
     * @return
     */
    public Map<Integer, ExceltitleDict> getExceltitleDictByTitleHead(List<String> titles){
        ExceltitleDictService exceltitleDictService = SpringUtil.getBean(ExceltitleDictService.class);
        Map<Integer, ExceltitleDict> map = exceltitleDictService.getExceltitleDictByTitleHead(titles, PackageNameEnum.CUSTOMERINFORMATIONINFO.getPackageName());
        return map;

    }

    public Map<String, Object> getResultMap() {
        return resultMap;
    }
    public void setParaMap(Map<String, Object> paraMap) {
        this.paraMap = paraMap;
    }

    public Map<String, Object> getParaMap() {
        return paraMap;
    }

    public List<String> getTradeCardcollect() {
        return TradeCardcollect;
    }

    public void setTradeCardcollect(List<String> tradeCardcollect) {
        TradeCardcollect = tradeCardcollect;
    }
}
